package demo;
import java.io.InputStream;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Enumeration;
import net.jxta.document.Document;
import net.jxta.document.Element;
import net.jxta.document.MimeMediaType;
import net.jxta.document.StructuredDocument;
import net.jxta.document.StructuredDocumentFactory;
import net.jxta.document.StructuredTextDocument;
import net.jxta.document.TextElement;
/**
* An example query response, which will be wrapped by a Resolver Response
continues
140 Chapter 5 The Peer Resolver Protocol
* Message to send the response to the query. The response contains the
* answer to the simple math question posed by the query.
*/
public class ExampleResponseMsg
{
/**
* The base from the original query.
*/
private double base = 0.0;
/**
* The power from the original query.
*/
private double power = 0.0;
/**
* The answer value for the response.
*/
private double answer = 0;
/**
* Creates a response object using the given answer value.
*
* @param anAnswer the answer for the response.
*/
	public ExampleResponseMsg(double aBase, double aPower, double anAnswer)
	{
		this.base = aBase;
		this.power = aPower;
		this.answer = anAnswer;
	}
/**
* Creates a response object by parsing the given input stream.
*
* @param stream the InputStream source of the response data.
* @exception IOException if an error occurs reading the stream.
*/
	public ExampleResponseMsg(InputStream stream) throws IOException
	{
		StructuredTextDocument document = (StructuredTextDocument)
		StructuredDocumentFactory.newStructuredDocument(
				new MimeMediaType("text/xml"), stream);
		Enumeration elements = document.getChildren();
		while (elements.hasMoreElements())
		{
			TextElement element = (TextElement) elements.nextElement();
			if(element.getName().equals("answer"))
			{
				answer = Double.valueOf(
						element.getTextValue()).doubleValue();
				continue;
			}
			if(element.getName().equals("base"))
			{
				base = Double.valueOf(element.getTextValue()).doubleValue();
				continue;
			}
			if(element.getName().equals("power"))
			{
				power = Double.valueOf(
						element.getTextValue()).doubleValue();
				continue;
			}
		}
	}
	/**
	 * Returns the answer for the response.
	 *
	 * @return the answer value for the response.
	 */
	public double getAnswer()
	{
		return answer;
	}
	/**
	 * Returns the base for the query.
	 *
	 * @return the base value for the query.
	 */
	public double getBase()
	{
		return base;
	}
/**
* Returns a Document representation of the response.
*
* @param asMimeType the desired MIME type representation for
* the response.
* @return a Document form of the response in the specified MIME
* representation.
*/
	public Document getDocument(MimeMediaType asMimeType)
	{
		Element element;
		StructuredDocument document = (StructuredTextDocument)
		StructuredDocumentFactory.newStructuredDocument(
				asMimeType, "example:ExampleResponse");
		element = document.createElement(
				"base", Double.toString(getBase()));
		document.appendChild(element);
		element = document.createElement("power",
				Double.toString(getPower()));
		document.appendChild(element);
		element = document.createElement("answer",
				(new Double(getAnswer()).toString()));
		document.appendChild(element);
		return document;
	}
/**
Listing 5.6 Continued
The Resolver Service 143
* Returns the power for the query.
*
* @return the power value for the query.
*/
	public double getPower()
	{
		return power;
	}
/**
* Returns an XML String representation of the response.
*
* @return the XML String representing this response.
*/
	public String toString()
	{
		try
		{
			StringWriter buffer = new StringWriter();
			StructuredTextDocument document = (StructuredTextDocument)
			getDocument(new MimeMediaType("text/xml"));
			document.sendToWriter(buffer);
			return buffer.toString();
		}
		catch (Exception e)
		{
			return "";
		}
	}

}